﻿/* 
Copyright (c) 2014 Eric Follea

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Facet4Windows
{
    public partial class TrayHolder : Form
    {
        private Environments envs;

        public TrayHolder()
        {
            InitializeComponent();
            this.Icon = Facet4Windows.Properties.Resources.TrayIcon;
            this.TrayIcon.Icon = this.Icon;
            this.TrayIcon.MouseClick += TrayIcon_MouseClick;
            this.envs = new Environments();
            envs.load();
        }

        void TrayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                this.WindowState = FormWindowState.Normal;
                this.Show();
            }
        }

        private void CloseApp(object sender, EventArgs e)
        {
            this.FormClosing -= TrayHolder_FormClosing;
            this.envs.stop();
            this.Close();
        }

        private void TrayHolder_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            this.WindowState = FormWindowState.Minimized;
        }

        private void TrayHolder_StyleChanged(object sender, EventArgs e)
        {
            this.ShowInTaskbar = FormWindowState.Normal == this.WindowState;
        }

        private void TrayHolder_Load(object sender, EventArgs e)
        {
            //  DirectoryInfo appData = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            DirectoryInfo facetData = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + Path.DirectorySeparatorChar + "Facet4Windows");
            if (!facetData.Exists)
            {
                facetData.Create();
            }

            FileInfo[] files = facetData.GetFiles("*.xml");
            if (files == null || files.Length == 0)
            {
                using (var source = new MemoryStream(Encoding.UTF8.GetBytes(Facet4Windows.Properties.Resources.SamplePAgent)))
                using (var dst = File.Create(facetData.FullName + Path.DirectorySeparatorChar + "sample.xml"))
                {
                    source.CopyTo(dst);
                }
              files =  facetData.GetFiles("*.xml");
            }

            //using (stream)
            //{
            //    envs.environments = new EnvironmentsFactory().from(stream).environments;
            //}
            Console.WriteLine(envs);

            var items = this.TrayContextMenu.Items;

            var count = items.Count;
            foreach (var file in files)
            {
                using (var stream = file.OpenRead())
                {
                    envs.environments = new EnvironmentsFactory().from(stream).environments;
                }
            }
            foreach (var env in envs.environments)
            {
                var item = new ToolStripButton(env.Name + " [" + env.Activated + "]");
                item.Tag = env;
                item.Click += new EventHandler(delegate(object o, EventArgs args)
                {
                    var environment = (EnvironmentFacet)item.Tag;
                    Console.WriteLine(environment.Name.ToString());
                    environment.switchState();
                });
                env.PropertyChanged += new PropertyChangedEventHandler(delegate(object o, PropertyChangedEventArgs args)
                {
                    item.Text = env.Name + " [" + env.Activated + "]";
                });
                env.OnActivated += this.envs.env_OnActivated;
                env.OnDeactivated += this.envs.env_OnDeactivated;



                items.Insert(items.Count - count, item);

            }

            items.Insert(items.Count - count, new ToolStripSeparator());

            envs.start();
        }

        private void OptionParameters(object sender, EventArgs e)
        {
            //test
        }

    }
}
